package com.team2.cyber_eye;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.team2.cyber_eye.ActivityHelpers.AdapterProduct;
import com.team2.cyber_eye.ActivityHelpers.Util;
import com.team2.shared.Product;

public class ProductList extends CyberActivity {

	ListView list;
	AdapterProduct adapter;
	
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState, R.layout.activity_product_list, "PRODUCT LIST");
		
		Bundle extras = this.getIntent().getExtras();
		if (extras != null)
		{
			ArrayList<Product> products = new ArrayList<Product>();
			try
			{
				products = (ArrayList<Product>)(Object)extras.getParcelableArrayList("products");
			}
			catch(Exception ex) {Util.LogError(ex); }
			
			list=(ListView)findViewById(R.id.productList);
		    adapter=new AdapterProduct(this, products);
		    
		    list.setAdapter(adapter);
		    list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> a, View v, int position, long id) { ItemClick(position); } });
		}
	}
	
	protected void ItemClick(int position)
	{
		try
		{
			Intent i = new Intent(this, ProductInfo.class);
	
			Object o = adapter.getItem(position);
			if (o != null)
			{
				i.putExtra("Product", (Parcelable)o);
				
				startActivity(i);
			}
		}
		catch (Exception ex)
		{
			Util.DisplayError(this, "Error loading product info", ex);
		}
	}
}
